package com.jxc.summer.controller;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import com.jxc.summer.dao.CableMapper;
import com.jxc.summer.service.CableService;
import com.jxc.summer.util.JacksonUtil;
import com.jxc.summer.util.ReturnT;
import com.jxc.summer.util.UUIDUtil;

@Controller
@RequestMapping("/cableinfo")
public class CableController {

	@Autowired
	private CableService cableService;
	@Autowired
	private CableMapper cableMapper;
	@RequestMapping
	public String index(Model model) {
		return "cable/cable.index";
	}
	
	@RequestMapping("/pageList")
	@ResponseBody
	public Map<String, Object> pageList(@SessionAttribute("accountCompanyCode") String companyCode,@RequestParam(required = false, defaultValue = "0") int start,  
			@RequestParam(required = false, defaultValue = "10") int length,@RequestParam String cablemodel,  
			@RequestParam String cablestandard) {
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("tc_cablemodel", cablemodel);
		params.put("tc_cablestandard", cablestandard);
		params.put("tc_companycode", companyCode);
		params.put("offset", start);
		params.put("pagesize", length);
		Map<String, Object> result = cableService.pageList(params);
		return result;
	}
	
	@RequestMapping("/addPage")
	public String addPage(Model model) {
		return "cable/cable.add";
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public ReturnT<String> addCable(@SessionAttribute("accountCompanyCode") String companyCode,@RequestParam String cablemodel,  
			@RequestParam String cablestandard,@RequestParam String cablestandards) {
		Map<String,Object> param = new HashMap<String, Object>();
		param.put("tc_cablemodel", cablemodel);
		param.put("tc_cablestandard", cablestandard);
		Set<Map<String,Object>>  cablestandardSet = JacksonUtil.readValue(cablestandards, Set.class);
		cablestandardSet.add(param);
		for (Map<String, Object> map : cablestandardSet) {
			map.put("tc_cableid", UUIDUtil.get32UUID());
			map.put("tc_companycode", companyCode);
		}
		int result = cableService.addCable(cablestandardSet);
		if(result>0){
			return ReturnT.SUCCESS;
		}else{
			return new ReturnT<String>(500, "新增线缆信息失败！");
		}
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public ReturnT<String> updateCable(@RequestParam(required = true) String id,
			@SessionAttribute("accountCompanyCode") String companyCode,@RequestParam String cablemodel,@RequestParam String cablestandard){
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("tc_cableid", id);
		params.put("tc_cablemodel", cablemodel);
		params.put("tc_cablestandard", cablestandard);
		params.put("tc_companyCode", companyCode);
		if(cableMapper.validateCable(params)>0){
			return new ReturnT<String>(500, "更新线缆信息失败,线缆规格重复。");
		}
		int result = cableService.updateCable(params);
		if(result>0){
			return ReturnT.SUCCESS;
		}else{
			return new ReturnT<String>(500, "更新线缆信息失败！");
		}
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public ReturnT<String> deleteCable(@RequestParam(required = true) String id){
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("tc_cableid", id);
		int result = cableService.deleteCable(params);
		if(result>0){
			return ReturnT.SUCCESS;
		}else{
			return new ReturnT<String>(500, "删除线缆信息失败！");
		}
	}
	@RequestMapping("/deleteBymodel")
	@ResponseBody
	public ReturnT<String> deleteBymodel(@RequestParam(required = true) String cablemodel,@SessionAttribute("accountCompanyCode") String companyCode){
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("tc_cablemodel", cablemodel);
		params.put("tc_companycode", companyCode);
		int result = cableService.deleteBymodel(params);
		if(result>0){
			return ReturnT.SUCCESS;
		}else{
			return new ReturnT<String>(500, "删除线缆信息失败！");
		}
	}
	
	@RequestMapping("getstandard")
	@ResponseBody
	public List<String> getstandard(@RequestParam(required = true) String model,@SessionAttribute("accountCompanyCode") String companyCode){
		return cableService.getstandard(model,companyCode);
	}
}
